<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="案件类型" prop="caseType">
                    <el-select
                        class="flex-1"
                        v-model="formData.caseType"
                        placeholder="请选择案件类型"
                    >
                        <el-option label="专利" value="1" />
                        <el-option label="商标" value="2" />
                        <el-option label="版权" value="3" />
                        <el-option label="科技服务" value="4" />
                        <el-option label="法律案件" value="5" />
                        <el-option label="调查案" value="6" />
                    </el-select>
                </el-form-item>
                <el-form-item label="文件类型" prop="fileType">
                    <el-select
                        class="flex-1"
                        v-model="formData.fileType"
                        placeholder="请选择文件类型"
                    >
                        <el-option label="官方来文" value="1" />
                        <el-option label="费用收据" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="文件命名" prop="fileRenamingRule">
                    <el-select
                        v-model="formData.fileRenamingRule"
                        placeholder="请选择文件命名"
                        class="flex-1 w-[285px]"
                        clearable
                        multiple
                    >
                        <template #label="{ label, value }">
                            <span>{{ label }}: </span>
                            <span style="font-weight: bold">{{ value }}</span>
                        </template>
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                    <!-- <el-select
            class="flex-1"
            v-model="formData.fileRenamingRule"
            placeholder="请选择文件命名规则"
          >
            <el-option label="请选择字典生成" value="" />
          </el-select> -->
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { renamingruleEdit, renamingruleAdd, renamingruleDetail } from '@/api/renamingrule'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑规则' : '添加规则'
})

const formData = reactive({
    id: '',
    caseType: '',
    fileType: '',
    fileRenamingRule: []
})

const fileRenamingRule = ref<string[]>(['Option1'])
const options = [
    {
        value: '1',
        label: '案件名称'
    },
    {
        value: '2',
        label: '客户文案'
    },
    {
        value: '3',
        label: '我方文号'
    },
    {
        value: '4',
        label: '申请号'
    },
    {
        value: '5',
        label: '文件描述'
    },
    {
        value: '6',
        label: '官方发文日'
    },
    {
        value: '7',
        label: '第一发明人'
    },
    {
        value: '8',
        label: '申请日'
    }
]
const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await renamingruleEdit(data) : await renamingruleAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await renamingruleDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
